package cn.edu.sjtu.se.dslab.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import cn.edu.sjtu.se.dslab.component.method.ComponentInterface;
import cn.edu.sjtu.se.dslab.component.method.InterfaceMethod;
import cn.edu.sjtu.se.dslab.dao.IBaseDao;
import cn.edu.sjtu.se.dslab.dao.IInterfaceMethodDao;

@Repository("interfaceMethodDao")
public class InterfaceMethodDaoImpl implements IInterfaceMethodDao {

	@Resource(name = "baseDao")
	private IBaseDao<InterfaceMethod> baseDao;

	@Override
	public void save(InterfaceMethod interfaceMethod) {
		// TODO Auto-generated method stub
		baseDao.save(interfaceMethod);
	}

	@Override
	public void update(InterfaceMethod interfaceMethod) {
		// TODO Auto-generated method stub
		baseDao.update(interfaceMethod);
	}

	@Override
	public InterfaceMethod queryById(long id) {
		// TODO Auto-generated method stub
		return baseDao.queryById(InterfaceMethod.class, id);
	}

	@Override
	public List<InterfaceMethod> queryByInterface(
			ComponentInterface componentInterface) {
		// TODO Auto-generated method stub
		// 定义存放hql语句的StringBuffer对象
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from InterfaceMethod interM where ");
		hql.append(" interM.cInterface.id=");
		hql.append("" + componentInterface.getId());
		return baseDao.query(hql.toString());
	}

	@Override
	public List<InterfaceMethod> queryAll() {
		// TODO Auto-generated method stub
		return baseDao.queryList("InterfaceMethod");
	}

	@Override
	public List<InterfaceMethod> queryWithInterfaceNotNull() {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from InterfaceMethod interM where ");
		hql.append(" interM.cInterface != null");
		return baseDao.query(hql.toString());
	}

	@Override
	public boolean deleteById(long id) {
		// TODO Auto-generated method stub
		InterfaceMethod method = queryById(id);
		method.getcInterface().getInterfaceMethods().clear();
		baseDao.delete(method);
		return true;
	}

	@Override
	public boolean delete(InterfaceMethod interfaceMethod) {
		// TODO Auto-generated method stub
		interfaceMethod.getcInterface().getInterfaceMethods().clear();
		baseDao.delete(interfaceMethod);
		return true;
	}

}
